<?php
/*
 * @Author: CHINAKAIYUAN<sunkaiyuan@namenode.cn>
 * @Date: 2022-02-02 19:42:06
 * @LastEditTime: 2023-10-26 15:19:03
 * @LastEditors: 孙开源 && sunkaiyuan@namenode.cn
 * @Description: 
 */

namespace App\Repositories;

use App\Models\ComprehensiveCar;
use App\Models\ComprehensiveCarDetails;
use Illuminate\Support\Facades\DB;

/**
 * 派车单
 * @property ComprehensiveCar $model
 * @property ComprehensiveCarDetails $detail
 * @author sunkaiyuan
 */
class ComprehensiveCarRepository extends Repositories
    {
    protected $relation_models = ['company', 'fauditor', 'status', 'department', 'creator', 'updator', 'status', 'attachments'];

    public function boot(ComprehensiveCar $model, ComprehensiveCarDetails $detail)
        {
        $this->model  = $model;
        $this->detail = $detail;
        }
    public function checkData($car_id)
        {
        if (is_numeric($car_id) && $car_id > 0) {
            $car_data = $this->whereId($car_id)->first();
            if ($car_data->status_id == 1) {
                fail("派车单已审核！禁止操作！");
                }
            if ($car_data == null) {
                fail("派车单不存在!");
                }
            return $car_data;
            }
        }
    public function addItem($source_addr, $target_addr, $code, $car_id, $model)
        {
        $new  = false;
        $main = $this->checkData($car_id);
        if ($main->status_id != 0) {
            fail("装卸单状态【{$main->status->name}】,新增明细失败！");
            }
        $car_item                 = $this->detail->firstOrCreate([
            'source_value' => $model->{$model->getcarSourceFieldKey()},
            'source_key'   => $model->getcarSourceFieldKey(),
            'source_model' => $model::class,
            'main_id'      => $car_id,
        ]);
        $quantity_field           = $model->getCarItemQuantityField();
        $auxiliary_quantity_field = $model->getCarItemAuxiliaryQuantityField();
        //数量是0
        if ($car_item->quantity == 0) {
            $new                      = true;
            $main->quantity += $model->{$quantity_field};
            $main->auxiliary_quantity += $model->{$auxiliary_quantity_field};
            }
        else {
            //数量更改
            if ($car_item->quantity != $model->{$quantity_field}) {
                $main->quantity           = $main->quantity - $car_item->quantity + $model->{$quantity_field};
                $main->auxiliary_quantity = $main->auxiliary_quantity - $car_item->auxiliary_quantity + $model->{$auxiliary_quantity_field};
                }
            else {
                //数量没有变
                return;
                }
            }
        if (isEmpty($main->billdate)) {
            $main->billdate = date("Y-m-d");
            }
        $main->save();
        $car_item->material_id = $model->material_id;
        $car_item->dyelot      = $model->dyelot;
        $car_item->target_code = $code;
        $car_item->type_id     = $model->type_id;
        $car_item->color_name  = $model->color_name;
        $car_item->quantity    = $model->{$quantity_field};

        $car_item->auxiliary_quantity = $model->{$auxiliary_quantity_field};
        $car_item->target_addr        = $target_addr;
        $car_item->source_addr        = $source_addr;
        $car_item->save();

        $model->{$model->getCarKey()} = $main->id;
        $model->save();

        return $new;
        }
    public function removeItem($car_id, $model)
        {

        $car_data = $this->checkData($car_id);

        $quantity_field           = $model->getCarItemQuantityField();
        $auxiliary_quantity_field = $model->getCarItemAuxiliaryQuantityField();
        //运输明细可能在之前就被删除了，不需要再更新运输总数
        if ($model->carItem) {
            $car_data->quantity           = $car_data->quantity - $model->{$quantity_field};
            $car_data->auxiliary_quantity -= $model->{$auxiliary_quantity_field};
            $car_data->save();
            $model->carItem->delete();
            }
        if ($model->exists) {
            $model->{$model->getCarKey()} = 0;
            $model->save();
            }
        return $model;
        }
    public function addCar($department_id, $company_id, $amount = 0, $remark = "")
        {
        return $this->model->create([
            'department_id' => $department_id,
            'company_id'    => $company_id,
            'amount'        => $amount,
            'remark'        => $remark,
        ]);
        }
    }